< previous page page_148 next page >

Page 148
Tip
If you are going to access all of the elements in a collection, the
For EachNext statement performs better than if you use the index values with a standard ForNext loop.

This loop statement can be used in conjunction with other object-related functions mentioned earlier today to access every control on a form in order to set their properties. As an example, if you had an application in which you wanted to set the font size of all the labels, command buttons, and text boxes to a specific size, you could do it in your application code with the following routine:
Dim myobj As Object
    For Each myobj In Screen.ActiveForm.Controls
        If TypeOf myobj Is TextBox or _
           TypeOf myobj Is CommandButton or _
           TypeOf myobj Is Label Then
           MyObj.FontSize = 10
        End If
    Next
This type of programming logic enables you to create single routines that will process any object on a form.
Conditional Loops
Just like the counter loops there are several different types of conditional loops that you can use: the Do and While loops. Each of them share one key feature, which is the condition. The condition is any valid Visual Basic expression that can be evaluated to True or False. The Do statement actually has two main types that you can use, the Do While and the Do Until. The following is the syntax for both of these statements:
Do [{While ¦ Until} condition]
[statements]
[Exit Do]
[statements]
Loop
As you can see, the Do statement syntax closely resembles the ForNext syntax, and in fact, the same principles apply.
The Do While statement tells the application to process the statements in the loop while a specified condition is true. When the condition becomes false, the loop is exited. The Do Until works the same way as the Do While, except that the loop is processed until a condition becomes true. This can be seen in the following two segments of code:

 
< previous page page_148 next page >

If you like this book, buy it!